/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : share  
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-18 11:09:34
 * @filename : static/mnm/src/share.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module) {
    
    var share = {};    
    module.exports = share;
   
    /** 
	 obj = {
	    type:
	     dom:
	     msg:
	    bool:
	 }

     */
    share.inputError = function(obj) {
         switch(obj.type) {
               //错误渲染方式
               case 1:
                     var dom = obj.dom;
		     if (dom.next().hasClass('err')) {
			 dom.next().remove();
			 dom.find('input').removeAttr('style').removeClass('input_err');
		     }

		     if (!obj.bool) {
			 dom.after("<div class='err'>"+obj.msg+"</div>");
			 dom.find('input').css('borderColor','#f00').addClass('input_err');
		     }

               break;
               case 2:
                     alert(obj.msg);
               break;
         }
    };

    share.pageLoad = function(bool,dom) {
         if (bool) {
             var html = "<div id='pageLoad'></div>";
             if ($('#pageLoad').length<1) {
                 dom.append(html);
             }
         } else {
                 dom.remove();
         }

         this.pageCover(bool,dom);
    };
    
    //type console.log/alert  
    share.error = function(type,msg,obj) {
         if (type == 'console') {
             console.log(msg); 
         } else {
             alert(msg);
         }
    };

    share.pageCover = function(bool,dom) {
	 if (bool) {
             var html = "<div id='pageLoadDiv'></div>";
             if ($('#pageLoadDiv').length<1) {
                 dom.append(html);
             }
         } else {
                 dom.remove();
         }

    };

    share.ajaxStartA = function(dom) {
           dom.parent().find('.ajaxLoading').remove();
           var html = $("<div class='ajaxLoading'></div>");
	   dom.after(html);
    };

    share.ajaxEndA = function(dom) {
           dom.parent().find('.ajaxLoading').remove();
    };

    share.ajaxStartB = function(dom) {
           var html = $("<div class='ajaxListLoading'></div>");
	       dom.append(html);
    };

    share.ajaxEndB = function(dom) {
           dom.find('.ajaxListLoading').remove();
    };

    share.ages = function(str) {   
        var r =  str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);     
        if(r==null)return false;     
        var d= new  Date(r[1],r[3]-1,r[4]);     
        if(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4])   
        {   
              var Y = new Date().getFullYear();   
              return (Y-r[1]);   
        }   
        return "error";   
    }; 
});



